<?php

namespace app\admin\controller\ques;

use app\common\controller\Backend;
use EasyWeChat\Factory;

/**
 * 小学领取-一轮复习
 *
 * @icon fa fa-circle-o
 */
class Xiaoxue extends Backend
{
    
    /**
     * Xiaoxue模型对象
     * @var \app\admin\model\activity\Xiaoxue
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\activity\Xiaoxue;
        //地区
        $this->assignconfig("arealist", $this->model->getProvinceOrderList());
        $this->view->assign("typeSchoolList", $this->model->getTypeSchoolList());
        $this->view->assign("paysList", $this->model->getPaysList());
        $this->view->assign("gradeList", $this->model->getGradeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function by_out($ids)
    {
        $config = [
            // 必要配置
            'app_id'             => 'wx1f8725eef4e232a4',
            'mch_id'             => '1502026301',
            'key'                => '7xuEuQHG1nyCihYc0NgU0QAuvQT4sHvt',   // API 密钥
            // 如需使用敏感接口（如退款、发送红包等）需要配置 API 证书路径(登录商户平台下载 API 证书)
            'cert_path'          => APP_PATH.'../extend/cert/apiclient_cert.pem', // XXX: 绝对路径！！！！
            'key_path'           => APP_PATH.'../extend/cert/apiclient_key.pem',      // XXX: 绝对路径！！！！
            'notify_url'         => '',     // 你也可以在下单时单独设置来想覆盖它
        ];
        $app = Factory::payment($config);
        $row = $this->model->get($ids);
        // 参数分别为：商户订单号、商户退款单号、订单金额、退款金额、其他参数
        $res = $app->refund->byOutTradeNumber($row->out_trade_no, 'hdyj'.time(), $row->sum*100, $row->sum*100,$config = []);

        if($res['return_code'] == 'SUCCESS')
        {
            $row->pays = 4;
            $row->save();
            $this->success("退款成功！<script>$('.btn-refresh').trigger('click')</script>");
        }
        if($res['err_code'] == 'ERROR')
        {
            $this->error($res['err_code_des']."<script>$('.btn-refresh').trigger('click')</script>");
        }



    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            $redis = getRedis();
            foreach ($list as $k => $v) {
                $redis->del('dt_'.$v['volume_id'].'_'.$v['openid']);
                $redis->del('form_'.$v['volume_id'].'_'.$v['openid']);
                $count += $v->delete();
            }
            if ($count) {

                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

}
